package cn.kailangtu.myapplication.camera;

import android.app.Activity;
import android.content.Context;
import android.os.SystemClock;
import android.util.Log;
import android.view.SurfaceHolder;

import androidx.annotation.NonNull;

import cn.kailangtu.myapplication.MyApplication;


public class AvcPlayerHelper {
    String TAG = "AvcPlayerHelper";
    private SurfaceHolder surfaceHolder;
    private AvcPlayer avcPlayer;
    SmIpCamera smIpCamera;
    private int RTSP_MS = 0;
    private boolean isOpen = false;
    private String urlRTSP = "rtsp://192.168.1.10:8554/profile0";

    private Activity context = null;
    public void initIPCameraView(AvcPlayer avcPlayer,String url,Activity context){
        this.avcPlayer = avcPlayer;
        this.urlRTSP = url;
        this.context = context;
        surfaceHolder = avcPlayer.getHolder();
        surfaceHolder.addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(@NonNull SurfaceHolder surfaceHolder) {
                Log.d(TAG,"surfaceCreated");
                cshAVCSmail1();
            }
            @Override
            public void surfaceChanged(@NonNull SurfaceHolder surfaceHolder, int i, int i1, int i2) {
                Log.d(TAG,"surfaceChanged");
            }
            @Override
            public void surfaceDestroyed(@NonNull SurfaceHolder surfaceHolder) {
                Log.d(TAG,"surfaceDestroyed");
                avcPlayer.stopPreview();
            }
        });
        //spy();
    }

    private void cshAVCSmail1(){
        Log.d(TAG,"cshAVC1");

        avcPlayer.init(avcPlayer.getWidth(), avcPlayer.getHeight(), new AvcPlayer.Callback() {
            @Override
            public void onFrameUpdate(byte[] frame) {
            }
        });
        avcPlayer.startPreview();
        smIpCamera = new SmIpCamera("1", urlRTSP, RTSP_MS, 20, avcPlayer);
        openCamera();
    }

    private void openCamera(){
        //showSmDialog("正在打开2号相机...");
        Log.d(TAG,"openCamera1");
        smIpCamera.openCamera(new SmIpCamera.CallbackIpcCamOpen() {
            @Override
            public void onOpenSuc(int errCode) {
                isOpen = true;
            }
            @Override
            public void onOpenFai(int errCode) {
            }
        });
    }

    public void pause(boolean isPause)
    {
        if(smIpCamera != null) {
            smIpCamera.setPause(isPause);
        }

    }

    public void close(){
        if(isOpen)
        {
            smIpCamera.CloseCamera();
        }
    }

    public void reOpen(){
        context.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                close();
                initIPCameraView(AvcPlayerHelper.this.avcPlayer,AvcPlayerHelper.this.urlRTSP,AvcPlayerHelper.this.context);
            }
        });

    }

    boolean isSpy = false;
    public  void spy()
    {
        new Thread(new Runnable() {
            @Override
            public void run() {
                if(isSpy == false){
                    isSpy = true;
                    while(isSpy)
                    {
                        if((System.currentTimeMillis() - SmIpCamera.lastReceiveTime) > 5000)
                        {
                            SmIpCamera.lastReceiveTime = System.currentTimeMillis();
                            reOpen();
                        }
                        SystemClock.sleep(1000);
                    }
                }
            }
        }).start();

    }
}
